package com.yanqu.road.dao.impl.wingroom;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.message.UserGiveLikeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/3/19 10:57
 */
public class WingRoomGiveLikeDaoImpl extends TempDataObjectDao<UserGiveLikeData> {

    public static String insertSql = "INSERT INTO t_u_wingroom_message_givelike(userId,dateMark,messageOwnerServerId,messageOwnerUserId) values(?,?,?,?)";
    @Override
    protected boolean addData(UserGiveLikeData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDateMark()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMessageOwnerServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMessageOwnerUserId()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(UserGiveLikeData data) {
        return false;
    }

    @Override
    protected UserGiveLikeData parseData(ResultSet rs) throws SQLException {
        UserGiveLikeData data = new UserGiveLikeData();
        data.setUserId(rs.getLong("userId"));
        data.setDateMark(rs.getInt("dateMark"));
        data.setMessageOwnerServerId(rs.getLong("messageOwnerServerId"));
        data.setMessageOwnerUserId(rs.getLong("messageOwnerUserId"));
        return data;
    }

    public Map<Integer, List<UserGiveLikeData>> getUserGiveLikeDataMap(long userId, int dateMark) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<UserGiveLikeData>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_wingroom_message_givelike where userId = ? and dateMark >= ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setInt(2, dateMark);

            rs = call.executeQuery();
            while (rs.next()) {
                UserGiveLikeData data = parse(rs);
                if (!dataMap.containsKey(data.getDateMark())) {
                    dataMap.put(data.getDateMark(), new ArrayList<>());
                }
                dataMap.get(data.getDateMark()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
